task-managment / src / app / dashboard / projects / [...id] / _components / AlertDeleteMaterial.tsx
AlertDeleteMaterial.tsx
Raw
"use client";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { useState } from "react";
import { MdDeleteOutline } from "react-icons/md";
import { trpc } from "@/app/_trpc/client";
import { SelectedMaterial } from "../types";
import Loading from "@/components/Loading";

const AlertDeleteMaterial = ({
  selectedMaterial,
}: {
  selectedMaterial: SelectedMaterial | null;
}) => {
  const [open, setOpen] = useState(false);
  const utils = trpc.useUtils();

  const { mutate, isPending } =
    trpc.materials.deleteProjectMaterial.useMutation({
      onSuccess: () => {
        setOpen(false);
        utils.projects.getProjectById.invalidate({
          value: selectedMaterial?.projectId!,
        });
      },
    });

  return (
    <AlertDialog open={open} onOpenChange={setOpen}>
      <AlertDialogTrigger>
        <MdDeleteOutline className="text-2xl text-red-600" />
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>
            Seguro que desea eliminar este articulo?
          </AlertDialogTitle>
          <AlertDialogDescription>
            Esta accion no puede ser reversible, y será borrada permanentemente
            de este projecto
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel className="border-none">
            <Button variant="link">Cancelar</Button>
          </AlertDialogCancel>
          <Button
            className="bg-primary text-white"
            disabled={isPending}
            onClick={() => {
              mutate({
                materialId: selectedMaterial?.materialId!,
                name: selectedMaterial?.materialName!,
                projectName: selectedMaterial?.projectName!,
                projectId:selectedMaterial?.projectId!
              });
            }}
          >
            <span className="mr-2">
              {" "}
              {isPending ? "Eliminando" : "Eliminar"}
            </span>
            {isPending && <Loading size={15} />}
          </Button>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
};

export default AlertDeleteMaterial;